import java.util.*;

public class Sort {
    public void sortColors(int[] nums) {
        int len=nums.length;
        int data[]=new int[3];
        for(int i=0;i<len;i++){
            data[nums[i]]++;
        }
        int mov=0;
        for (int i=0;i<data[0];i++,mov++){
            nums[mov]=0;
        }
        for (int i=0;i<data[1];i++,mov++){
            nums[mov]=1;
        }
        for (int i=0;i<data[2];i++,mov++){
            nums[mov]=2;
        }
    }

    public int findKthLargest(int[] nums, int k) {
        Arrays.sort(nums);
        return nums[nums.length-k+1];
    }

    public int findPeakElement(int[] nums) {
        int len=nums.length;
        if(len==1)return 0;
        for (int i=0;i<len-1;i++){
            if(i==0&&nums[i]>nums[i+1])return i;
            else if(i==0) continue;
            else if(i==len-2&&nums[i]<nums[i+1])return i+1;
            else {
                if(nums[i]>nums[i-1]&&nums[i]>nums[i+1])return i;
            }
        }
        return len-1;
    }


    public List<Integer> topKFrequent(int[] nums, int k) {
        List<Integer> res = new ArrayList<>();
        Map<Integer,Integer> map = new TreeMap<>();
        for(int i = 0;i<nums.length;i++){
            if(map.containsKey(nums[i])){
                map.put(nums[i],map.get(nums[i]).intValue()+1);
            }else{
                map.put(nums[i],1);
            }
        }
        List<Map.Entry<Integer,Integer>> list = new ArrayList<Map.Entry<Integer,Integer>>(map.entrySet());
        //然后通过比较器来实现排序
        Collections.sort(list,new Comparator<Map.Entry<Integer,Integer>>() {
            //降序
            public int compare(Map.Entry<Integer, Integer> o1,
                               Map.Entry<Integer, Integer> o2) {
                return o2.getValue().compareTo(o1.getValue());
            }

        });
        for(Map.Entry<Integer,Integer> mapping:list){
            res.add(mapping.getKey());
            if(res.size()==k){
                break;
            }
        }
        return res;
    }

    public int[] searchRange(int[] nums, int target) {
        int start=0;
        int end=nums.length-1;
        int mid=(end-start)/2+start;
        int []res={-1,-1};
        while (start<=end){
            mid=(end-start)/2+start;
            if(nums[mid]<target){
                start=mid+1;
            }else if(nums[mid]>target){
                end=mid-1;
            }else {
                break;
            }
        }
        if(start<=end){
            int i=mid,j=mid;
            for(;i>=0;i--){
                if(nums[i]!=target)break;
            }
            res[0]=i+1;
            for(;j<nums.length;j++){
                if(nums[j]!=target)break;
            }
            res[1]=j-1;
        }
        return res;
    }

    public List<Interval> merge(List<Interval> intervals) {
        Collections.sort(intervals,(Interval a,Interval b)->(a.start-b.start));
        int i = 1;
        while(i < intervals.size()){
            if(intervals.get(i).start <= intervals.get(i - 1).end){
                intervals.get(i - 1).end = Math.max(intervals.get(i - 1).end, intervals.get(i).end);
                intervals.remove(i);
            }
            else i++;
        }
        return intervals;
    }

    public int search(int[] nums, int target) {
        if(nums.length==0){
            return -1;
        }
        int st = 0,end = nums.length-1;
        while(st <= end){
            int mid = st+(end-st)/2;
            if(nums[mid]==target){
                return mid;
            }
            if(nums[mid]>=nums[st]){
                if(nums[st]<=target&&target<nums[mid]){
                    end = mid-1;
                }else{
                    st = mid+1;
                }
            }else{
                if(nums[mid]<target&&target<=nums[end]){
                    st = mid+1;
                }else{

                    end = mid==0?mid:mid-1;
                }
            }
        }
        return -1;
    }
    public boolean searchMatrix(int[][] matrix, int target) {
        if(matrix.length==0) return false;
        int H=matrix.length,W=matrix[0].length;
        int i=H-1,j=0;
        while (i>=0&&j<W){
            if(matrix[i][j]==target){
                return true;
            }else if(matrix[i][j]<target){
                j++;
            }else {
                i--;
            }
        }
        return false;
    }

}
